//
//  MainTabBarController.swift
//

import UIKit

class MainTabBarController: UITabBarController {

    private lazy var mainTabBar: MainTabBar = {
        let view = MainTabBar()
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setValue(mainTabBar, forKey: "tabBar")
        
        let recVC = HomeViewController()
        let rechNav = configureNav(recVC, title: "首页", image: "tabbar_home_normal".image, selectedImage: "tabbar_home_selected".image)
        
        let songVC = SchoolViewController()
        let songNav = configureNav(songVC, title: "学堂", image: "tabbar_course_normal".image, selectedImage: "tabbar_course_selected".image)
        
        let choiceVC = ChoiceViewController()
        let choiceNav = configureNav(choiceVC, title: "VIP", image: nil, selectedImage: nil)
        
        let paradiseVC = GrowthParadiseViewController()
        let paradiseNav = configureNav(paradiseVC, title: "成长乐园", image: "tabbar_daka_normal".image, selectedImage: "tabbar_daka_selected".image)
        
        let profileVC = ProfileViewController()
        let profileNav = configureNav(profileVC, title: "我的", image: "tabbar_me_normal".image, selectedImage: "tabbar_me_selected".image)
        
        viewControllers = [rechNav, songNav, choiceNav, paradiseNav, profileNav]
    }

    private func configureNav(_ viewController: UIViewController,
                              title: String,
                              image: UIImage?,
                              selectedImage: UIImage?) -> UINavigationController {
        let nav = BaseNavigationController(rootViewController: viewController)
        nav.navigation.configuration.isEnabled = true
        nav.navigation.configuration.isTranslucent = true
        nav.navigation.configuration.barTintColor = UIColor.white
        nav.navigation.configuration.isShadowHidden = true
        nav.navigation.configuration.titleTextAttributes =
            [.foregroundColor: UIColor.sysBlack,
             .font: 17.font]
    
        nav.tabBarItem.setTitleTextAttributes([.foregroundColor: UIColor.c999999, .font: UIFont.systemFont(ofSize: 10)], for: .normal)
        nav.tabBarItem.setTitleTextAttributes([.foregroundColor: UIColor.titleColor, .font: UIFont.systemFont(ofSize: 10)], for: .selected)
         nav.tabBarItem.title = title
        nav.tabBarItem.image = image
        nav.tabBarItem.selectedImage = selectedImage
        
        if #available(iOS 13.0, *) {
            tabBar.unselectedItemTintColor = UIColor.black
        }
        
        return nav
    }

    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        mainTabBar.updateVip(item.title == "VIP")
    }
}
